//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.MemberTransfer;
import com.aidong.model.wills.PayList;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class MemberTransferDao extends BaseDao<MemberTransfer, Integer> {
	public MemberTransferDao() {
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.saleId, c.shopCode, c.saleDate, c.contractCode, c.memberCode, i.chName memberName, c.cardCode\n, c.status, c.addFee, c.payAmount, k.beginTime, k.endTime, k.cardType\n, c.newShopCode, c.newContractCode, c.salesCode, c.salesCode2\nfrom MemberChangeShop c inner join MemberContract k on c.contractCode=k.contractCode\ninner join MemberInfo i on c.memberCode=i.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by c.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public void addTransfer(MemberTransfer transfer, List<PayList> payList, MemberContract newContract, MemberInfo member) {
		String shopCode = newContract.getShopCode();
		String sql = "select max(contractCode) from MemberContract where shopCode=:p1 and contractCode like :p2";
		String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
		int maxId = 0;
		if(maxCode != null && maxCode.length() > shopCode.length()) {
			try {
				maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
			} catch (Exception var15) {
				;
			}
		}

		String contractCode = "000000" + (maxId + 1);
		contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
		String memberCode = member.getMemberCode();
		if(memberCode == null || memberCode.length() == 0) {
			SimpleDateFormat memberName = new SimpleDateFormat("yyMMdd");
			String sign = shopCode + memberName.format(new Date());
			sql = "select max(memberCode) from MemberInfo where shopCode=:p1 and memberCode like :p2";
			maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, sign + "%"});
			maxId = 0;
			if(maxCode != null && maxCode.length() > shopCode.length()) {
				try {
					maxId = Integer.parseInt(maxCode.substring(sign.length()));
				} catch (Exception var14) {
					;
				}
			}

			++maxId;
			memberCode = "0000" + maxId;
			memberCode = sign + memberCode.substring(memberCode.length() - 4);
		}

		if(member.getCardCode() == null || member.getCardCode().length() == 0 || !member.getCardCode().equalsIgnoreCase(transfer.getNewCardCode())) {
			member.setCardCode(transfer.getNewCardCode());
			sql = "update MemberCard set memberCode=:p1, contractCode=:p2, status=2, useUser=:p3, useDate=getdate() where cardCode=:p4";
			this.updateBySql(sql, new Object[]{memberCode, contractCode, transfer.getCreateUser(), member.getCardCode()});
		}

		if(member.getMemberCode() != null && member.getMemberCode().length() != 0) {
			sql = "update MemberInfo set cardCode=:p1, memberPhoto=:p2, idCardPhoto=:p3, updateTime=getdate() where memberCode=:p4";
			this.updateBySql(sql, new Object[]{member.getCardCode(), member.getMemberPhoto(), member.getIdCardPhoto(), memberCode});
		} else {
			member.setMemberCode(memberCode);
			this.getSession().save(member);
		}

		transfer.setNewContractCode(contractCode);
		transfer.setNewMemberCode(memberCode);
		this.add(transfer);
		Iterator var16 = payList.iterator();

		while(var16.hasNext()) {
			PayList var18 = (PayList)var16.next();
			var18.setSaleId(transfer.getSaleId());
			var18.setContractCode(transfer.getContractCode());
			this.getSession().save(var18);
			if("S011".equalsIgnoreCase(var18.getPayType())) {
				sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sql, new Object[]{Integer.valueOf(2), var18.getAutoId(), var18.getSalesCode(), var18.getSalesCode2(), new Date(), var18.getNote()});
			}
		}

		sql = "update MemberContract set effectiveDate=:p1, effectiveType=:p2, outAmount=:p3, updateTime=:p4 where contractCode=:p5";
		this.updateBySql(sql, new Object[]{newContract.getBeginTime(), "4", newContract.getInAmount(), new Date(), transfer.getContractCode()});
		newContract.setContractCode(contractCode);
		newContract.setMemberCode(memberCode);
		newContract.setCardCode(member.getCardCode());
		this.getSession().save(newContract);
		sql = "insert into ContractMember (ContractCode, MemberCode) values (:p1, :p2)";
		this.updateBySql(sql, new Object[]{contractCode, transfer.getNewMemberCode()});
		sql = "insert into ContractShop (ContractCode, ShopCode) select \'" + contractCode + "\',shopCode from ContractShop where contractCode=:p1";
		this.updateBySql(sql, new Object[]{transfer.getContractCode()});
		if(transfer.getEarnestId() != null) {
			sql = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(sql, new Object[]{transfer.getSaleId(), transfer.getEarnestId()});
		}

		String var17 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{transfer.getMemberCode()});
		ContractSign var19 = new ContractSign();
		var19.setShopCode(transfer.getShopCode());
		var19.setContractCode(transfer.getContractCode());
		var19.setContractType("05");
		var19.setSaleId(transfer.getSaleId());
		var19.setMemberCode(transfer.getMemberCode());
		var19.setMemberName(var17 == null?"":var17);
		var19.setStatus(Integer.valueOf(1));
		var19.setSalesCode(transfer.getSalesCode());
		var19.setSalesCode2(transfer.getSalesCode2());
		var19.setCreateUser(transfer.getCreateUser());
		var19.setCreateTime(new Date());
		var19.setUpdateTime(new Date());
		this.getSession().save(var19);
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(newContract.getShopCode());
		sign1.setContractCode(contractCode);
		sign1.setContractType("01");
		sign1.setSaleId(newContract.getSaleId());
		sign1.setMemberCode(member.getMemberCode());
		sign1.setMemberName(member.getChName());
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(newContract.getSalesCode());
		sign1.setSalesCode2(newContract.getSalesCode2());
		sign1.setCreateUser(newContract.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}
}
